//
//  ScientificKeypad.m
//  iCalculator
//
//  Created by Stephen Houser on 9/13/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "CalculatorApp.h"
#import "ScientificKeypad.h"

@implementation ScientificKeypad

/*
 * Initialize the keypad to live in the given frame.
 * This routine should set up all that needs to be setup
 * for the keypad to start working.
 */
- (id) initWithFrame: (CGRect)frame withController: (ScientificCalculatorView *) theController {
	self = [super initWithFrame: frame];

	// init our controller reference
	controller = theController;
	
	// set up to get Tap events (handleTapWithCount)
	[self setTapDelegate: self];
	
	return self;
} 

/*
 * Utility routine to decode what key was pressed.
 * it is very rudimentary and based purely on observation
 * and a little math applied to the keypad image that
 * Apple uses on their iPhone calculator.
 */
- (NSString *) decodeKeypress: (CGPoint)where {
	// and here is the little bit of math and observation
	// keypad is 320 x 371
	// keys are 80 x 74

	// the keys are arranged as such on the keyboard
	char * keys[8][6] = {
		{ "",          "",       "",    "",    "",   "" },
		{ "",          "",       "",    "",    "",   "" },
		{ "sin",       "cos",    "tan", "log", "",   "%" },
		{ "yx",        "ex",     "m+",  "m-",  "mr", "/" }, 
		{ "factorial", "ln",     "7",   "8",   "9",  "*" },
		{ "sqrt",      "invert", "4",   "5",   "6",  "+" },
		{ "square",    "cube",   "1",   "2",   "3",  "-" },
		{ "pi",        "e",      "0",   ".",   "c",  "=" }
	};

	// cheap trick
	NSLog(@"decodeKeypress: where.x=%g, where.y=%g", where.x, where.y);
	int x = (int)where.x / 53;
	int y = (int)(where.y - 89) / 46;
	NSLog(@"decodeKeypress: x=%d, y=%d", x, y);
	
	// table lookups are my favorite.
	NSString *key = [[NSString alloc] initWithCString: keys[y][x]];
	NSLog(@"keypress: ==> %@", key);
	return key;
}

/* 
 * Called when the finger goes down.
 * Currently does nothing. Will need to "highlight" the button
 * or key the person is pressing.
 */
- (void) mouseDown:(GSEvent *)gsEvent {
	//CGRect rect = GSEventGetLocationInWindow(gsEvent);
	//CGPoint point = rect.origin;
	//NSLog(@"mouseDown: event:(point.x=%f, point.y=%f)",point.x, point.y);
}

/* 
 * Called when the finger comes up.
 * Currently does nothing. Will need to un "highlight" the button
 * or key the person is pressing.
 */
- (void) mouseUp:(GSEvent *)gsEvent {
	//CGRect rect = GSEventGetLocationInWindow(gsEvent);
	//CGPoint point = rect.origin;
	//NSLog(@"mouseUp: event:(point.x=%f, point.y=%f)",point.x, point.y);
}

/*
 * Handle the user tapping a place on the screen. This makes life easy
 * for a calculator anyway. It appears this will handle those bad dragging
 * events and let me know when someone hits a key -- or taps it properly.
 * So here is where we capture keypresses and pass them to the controller.
 */
- (void) view:(UIView *) view handleTapWithCount:(int)count event:(GSEvent *)event {
	CGRect rect = GSEventGetLocationInWindow(event);
	CGPoint point = rect.origin;
	//NSLog(@"view:%@ handleTapWithCount:%i event:(point.x=%f, point.y=%f)", view, count, point.x, point.y);
	
	// send the decoded command to my controller
	NSString *command = [self decodeKeypress: point];
	[controller handleCommand: command];	
}

@end
